package com.seven.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.codehaus.jackson.map.ObjectMapper;

import com.seven.entity.Problem;
import com.seven.service.AProblemService;

@WebServlet("/problem")
public class AProblemServlet extends BaseServlet{
    AProblemService problemService = new AProblemService();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        List<Problem> problems = problemService.selectAllProblem();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(problems);
//        String json = JSON.toJSON(problems).toString();
        System.out.println(json);
        out.print(json);
    }


    public void selectAllByWorkerID(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        Problem problem = new Problem();
        String worker_id = request.getParameter("worker_id");
        System.out.println(worker_id);
        problem.setWorker_id(Integer.parseInt(worker_id));
        System.out.println(problem);
        List<Problem> problems = problemService.selectByWorker_id(problem);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(problems);
        System.out.println(json);
        out.print(json);
    }



    public void selectAllByID(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        Problem problem = new Problem();
        String users_id = request.getParameter("users_id");
        System.out.println(users_id);
        problem.setUsers_id(Integer.parseInt(users_id));
        List<Problem> problems = problemService.selectByUsers_id(problem);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(problems);
//        String json = JSON.toJSON(problems).toString();
        System.out.println(json);
        out.print(json);
    }

    public void addxg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //文件上传
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        Problem problem = new Problem();
        problem.setProblem_isauditor(1);
        problem.setProblem_ispass(0);
        try {
            //解析请求
            List<FileItem> list = upload.parseRequest(request);
            for (FileItem fileItem : list) {
                if(fileItem.isFormField()){//是否为表单普通文本类型
                    String name = fileItem.getFieldName();
                    if("date".equals(name)){
                        String date = fileItem.getString("utf-8");
//                        2018-07-08 13:48:08
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        problem.setProblem_time(sdf.parse(date));
                    }else if("worker_id".equals(name)){
                        String worker_id = fileItem.getString("utf-8");
                        problem.setWorker_id(Integer.parseInt(worker_id));
                    }else if("problem_id".equals(name)){
                        String problem_id = fileItem.getString("utf-8");
                        problem.setProblem_id(Integer.parseInt(problem_id));
                    }
                }else{
                    //获取文件名
                    String fileName = fileItem.getName();
                    System.out.println(fileName);
                    problem.setProblem_picture2("picture/"+fileName);
                    //获取路径
                    String path = getServletContext().getRealPath("picture");
                    File file =new File(path);
                    if (!file.exists()){
                        file.mkdirs();
                    }
                    //文件类型
                    File saveFile = new File(path,fileName);
                    fileItem.write(saveFile);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        problemService.updatePromblem1(problem);
        System.out.println(problem);
    }




    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //文件上传
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        Problem problem = new Problem();
        problem.setProblem_isauditor(0);
        problem.setProblem_ispass(-1);
        try {
            //解析请求
            List<FileItem> list = upload.parseRequest(request);
            for (FileItem fileItem : list) {
                if(fileItem.isFormField()){//是否为表单普通文本类型
                    String name = fileItem.getFieldName();
                    if("title".equals(name)){
                        String title = fileItem.getString("utf-8");
                        problem.setProblem_title(title);
                    }else if("address".equals(name)){
                        String address = fileItem.getString("utf-8");
                        problem.setProblem_address(address);
                    }else if("bickunm".equals(name)){
                        String bickunm = fileItem.getString("utf-8");
                        problem.setProblem_bickunm(bickunm);
                    }else if("content".equals(name)){
                        String content = fileItem.getString("utf-8");
                        problem.setProblem_content(content);
                    }else if("date".equals(name)){
                        String date = fileItem.getString("utf-8");
//                        2018-07-08 13:48:08
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        problem.setProblem_time(sdf.parse(date));
                    }else if("users_id".equals(name)){
                        String users_id = fileItem.getString("utf-8");
                        problem.setUsers_id(Integer.parseInt(users_id));
                    }
                }else{
                    //获取文件名
                    String fileName = fileItem.getName();
                    System.out.println(fileName);
                    problem.setProblem_picture("picture/"+fileName);
                    //获取路径
                    String path = getServletContext().getRealPath("picture");
                    File file =new File(path);
                    if (!file.exists()){
                        file.mkdirs();
                    }
                    //文件类型
                    File saveFile = new File(path,fileName);
                    fileItem.write(saveFile);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        problemService.addProblem(problem);
        System.out.println(problem);
    }
}
